

/**
 * @author Team 29
 * This class stores all properties associated with a loginable 
 * user, including user type, login credentials, all associated 
 * hospital documentation, etc.
 *
 */
public class User {
	
	
protected String userID;
protected String patienttype;
protected String firstName;
protected String lastName;
protected String loginName;
protected String loginPassword;
protected String contactinfo;
protected BasicInfo bi;
protected Invoice invo;
protected MedicalHistory mh;
protected DoctorsOrders docord;
protected TreatmentRecord tr;
protected BillingInformation bill;
protected String apptreq;

private int failcount=0;

/**
 * increments the number of failed logins for this user. suspends
 * after 3.
 */
public void fail()
{
	failcount++;
	if(failcount == 3)
	{
		loginName = "SUSPENDED" + loginName;
	}
}

/**
 * unsuspends the account. 
 */
public void unfail()
{
	loginName = loginName.replaceFirst("SUSPENDED", "");
	failcount = 0;
}

public String getLoginName(){
	return this.loginName;
}
public void setLoginName(String name){
	this.loginName = name;
}
public String getLoginPassword(){
	return this.loginPassword;
}
public void setLoginPassword(String password){
	this.loginPassword = password;
}

/**Creates a new instance of User after a successful login.
 *  
 * @param firstName
 * @param lastname
 * @param userID UserID is passed into the constructor for user
 * 			after a validated successful login.
 */
public User(String fn, String ln, String uID, String type){
	
	this.firstName = fn;
	this.lastName = ln;
	this.userID = uID;
	this.patienttype = type;
	this.contactinfo= " ";
	this.loginName=" ";
	this.loginPassword=" ";
	bi = new BasicInfo(fn + " " + ln, "DOB", "AGE", "HGHT", "WGHT");
	bill = new BillingInformation(fn+"_"+ln,"Billing_Adress","CC_Number");
	invo = new Invoice("None", "N/A");
	mh = new MedicalHistory(uID+"mh.txt");
	tr = new TreatmentRecord(uID+"tr.txt");
	docord= new DoctorsOrders("prepscription", "labwork", "followUp", "other");
	apptreq = "0";
}

public String getUserID() {
	return userID;
}
public String getFirstName() {
	return firstName;
}
public String getLastName() {
	return lastName;
}


public String toString(){
	return lastName + ", " + firstName + 
		" ID Number: " + userID +
		" Type: " + patienttype;
}

public String toSaveString(){
	String s="";
	s+=userID + "\t" +
		patienttype + "\t" +
		firstName + "\t" +
		lastName + "\t" +
		loginName+ "\t" +
		loginPassword + "\t" +
		contactinfo+ "\t" +
		apptreq + "\r\n" +
		bi.toString() + "\r\n" +
		mh.toString() + "\r\n" +
		docord.toString() + "\r\n" +
		tr.toString() + "\r\n" +
		bill.toString() + "\r\n" +
		invo.toString();
	return s;
}
	public User(String userID, String patienttype,
			String firstName, String lastName,
			String loginName, String loginPassword,
			String contactinfo, String apptreq, BasicInfo bi,
			MedicalHistory mh, DoctorsOrders docord,
			TreatmentRecord tr, BillingInformation bill, Invoice invo){
		this.userID=userID;
		this.patienttype=patienttype;
		this.firstName=firstName;
		this.lastName=lastName;
		this.loginName=loginName;
		this.loginPassword=loginPassword;
		this.contactinfo=contactinfo;
		this.apptreq = apptreq;
		this.bi=bi;
		this.mh=mh;
		this.docord=docord;
		this.tr=tr;
		this.bill=bill;
		this.invo = invo;
		apptreq = "0";
	}
	
	public static User fromString(String userString){
		String[] args = userString.split("\r\n");
		User user = null;
		try{
			String[] args2 = args[0].split("\t");
			BasicInfo bi = BasicInfo.fromString(args[1]);
			MedicalHistory mh = MedicalHistory.fromString(args[2]);
			DoctorsOrders docord = DoctorsOrders.fromString(args[3]);
			TreatmentRecord tr = TreatmentRecord.fromString(args[4]);
			BillingInformation bill = BillingInformation.fromString(args[5]);
			Invoice invo = Invoice.fromString(args[6]);
			user = new User(args2[0], args2[1], args2[2], args2[3],
					args2[4], args2[5], args2[6], args2[7], bi, mh, docord, tr, bill, invo);
		}
		catch (Exception e){
			System.out.println("Error parsing user: " + userString);
			Logger.log("Error parsing user: " + userString);
		}
		return user;
	}

}
